package com.school.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.school.entity.HljtUser;
import com.school.entity.web.UseresloginParameter;
import com.school.service.impl.HljtUserServiceImpl;
import com.school.util.JsonUtil;
import com.school.util.RepUtil;


@Controller
@RequestMapping(value="userController")
public class HljtUserController {

	@Autowired
	private HljtUserServiceImpl oService;
	/**
	 * 用户登录
	 * @param oHljtUser
	 * @param req
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/userLogin")
	@ResponseBody
	public String userLogin(HljtUser oHljtUser,HttpServletRequest req,HttpServletResponse rep){
		//RepUtil.repPake(rep);
		HttpSession oSession=req.getSession();
		oSession.setAttribute("username",oHljtUser.getUsername() );
		return oService.userlogin(oHljtUser);
	}
	/**
	 * 用户登录权限
	 * @param oHljtUser
	 * @param req
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/useresLogin")
	@ResponseBody
	public JSONObject useresLogin(HljtUser oHljtUser,JsonUtil jsonUtil,HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse){
		RepUtil.repPake(httpServletResponse);
		HttpSession oSession=httpServletRequest.getSession(true);
		UseresloginParameter useresloginParameter = oService.usereslogin(oHljtUser);
		oSession.setAttribute(useresloginParameter.getId()+"",useresloginParameter);
		//UseresloginParameter d = (UseresloginParameter) oSession.getAttribute("username");
		//oSession.removeAttribute("username");
		//jsonUtil.setSucceed(JSON.toJSONString(d) ,"登陆信息");
		JSONObject ret=new JSONObject();
		ret.put("data", useresloginParameter);
		return ret ; //jsonUtil.toJSONString();
	}
	
	
	/**
	 * 登录的用户权限
	 * @param oHljtUser
	 * @param req
	 * @param rep
	 * @return
	 */
	@RequestMapping(value="/loginJurisdiction")
	@ResponseBody
	public JSONObject loginJurisdiction(Integer teacherId,Integer pageId,JsonUtil jsonUtil,HttpServletResponse httpServletResponse){
		RepUtil.repPake(httpServletResponse);
		
		JSONObject ret=new JSONObject();
		ret.put("data", oService.getPermissionpage(teacherId, pageId));
		return ret ; //jsonUtil.toJSONString();
	}
	
	
	

	/**
	 * 修改用户
	 * @param oHljtUser
	 * @return
	 */
	@RequestMapping(value="/userUpdate")
	@ResponseBody
	public int userUpdate(HljtUser oHljtUser){
		return oService.userUpdate(oHljtUser);
	}
	/**
	 * 添加用户
	 * @param oHljtUser
	 * @return
	 */
	@RequestMapping(value="/userInsert")
	@ResponseBody
	public int userInsert(HljtUser oHljtUser){
		return oService.userInsert(oHljtUser);
	}
	/**
	 * 删除用户
	 * @param username
	 * @return
	 */
	@RequestMapping(value="/userDelete")
	@ResponseBody
	public int userDelete(String username){
		return oService.userDelete(username);
	}
}
